package com.qidian.test;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;

public class Main
{
    //prime number no more than 100
    static int[] prime_numbers = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97} ;
    
    // prime numbers(no more than 10000) bit map 
    static byte[] prime_number_bit_map = new byte[10000/8] ;
    
    
    /**
     * 
     * */
    public static void setBit(byte[] bitmap, int position, boolean bit)
    {
        if (position >= bitmap.length*Byte.SIZE || position < 0)
            return ;
        
        int bytePosition = position/Byte.SIZE ;
        int bitPosition = Byte.SIZE - 1 - (position%Byte.SIZE) ;
        
        if (bit)
            bitmap[bytePosition] = (byte)(bitmap[bytePosition] | 1<<bitPosition);
        else
            bitmap[bytePosition] =  (byte)(bitmap[bytePosition] & ~(1<<bitPosition));
    }
    public static void setBit(int[] bitmap, int position, boolean bit)
    {
        if (position >= bitmap.length*Integer.SIZE || position < 0)
            return ;
        
        int bytePosition = position/Integer.SIZE ;
        int bitPosition = Integer.SIZE -1 - (position%Integer.SIZE) ;
        
        if (bit)
            bitmap[bytePosition] = bitmap[bytePosition] | 1<<bitPosition;
        else
            bitmap[bytePosition] = bitmap[bytePosition] & ~(1<<bitPosition);
    }
    public static boolean getBit(byte[] bitmap, int position)
    {
        assert(position < bitmap.length && position >=0) ;
        
        int bytePosition = position / Byte.SIZE ;
        int bitPosition = Byte.SIZE - (position % Byte.SIZE) - 1;
        return (bitmap[bytePosition] & (1<<bitPosition)) > 0 ;
    }
    public static boolean getBit(int[] bitmap, int position)
    {
        assert(position < bitmap.length && position >=0) ;
        
        int bytePosition = position / Integer.SIZE ;
        int bitPosition = Integer.SIZE - (position % Integer.SIZE) - 1;
        return (bitmap[bytePosition] & (1<<bitPosition)) > 0 ;
    }    
    
    
    public static void initPrimeNumberBitMap(byte[] bitmap)
    {
        for (int i = 0 ; i < bitmap.length*8 ; i ++)
            setBit(bitmap, i, isPrimeNumber(i+1)) ;
        return ;
    }
    public static void printPrimeNumberPairs(byte[] bitmap)
    {
        for (int i = 6 ; i <= 10000  ; i = i + 2)
        {
            // 2+x = i
            if (getBit(bitmap, i-2+1))
                System.out.println(i+" = "+(i-2)+"+2");
            
            // x+y = i
            for (int j = 3 ; j <= i/2 ; j = j + 2)
            {
                if (getBit(bitmap, j-1) && getBit(bitmap, i-j-1))
                {
                    System.out.println(i+" = "+j+"+"+(i-j));
                    break ;
                }
            }
        }
    }
    
    public static void printPrimeNumber(int num)
    {
        int count = 0 ;
        for (int i = 1 ; i < num ; i ++)
        {
            if (isPrimeNumber(i))
            {
                System.out.print(i);
                count ++ ;
                if (count % 10 == 9)
                    System.out.println();
                else
                    System.out.print(',');
            }
        }
        System.out.println("\nCount:"+count) ;
    }
    public static void main(String[] args) throws UnsupportedEncodingException
    {
//        char[] string = "abcdefg".toCharArray() ;
//        move(string, 3) ;
//        System.out.println(string) ; 
//        String aString = (String)null ;
//        
//        printPrimeNumber(100) ;
//        initPrimeNumberBitMap(prime_number_bit_map) ;
//        printPrimeNumberPairs(prime_number_bit_map) ;
//    	UUID ss = UUID.randomUUID();
//    	System.out.println(ss.toString()) ;
//    	System.out.println(ss.toString().replaceAll("-", "")) ;
//    	
//    	String userName = "123345" ;
//    	String password = "123233" ;
//		if (userName.matches("[0-9a-zA-Z_]{6,20}") && password.matches("[0-9a-zA-Z_]{6,20}"))
//			System.out.println("1") ;
//		else
//			System.out.println("2") ;
//		System.out.println("1".matches("[0-9](1,2)")) ;
//    	String test = "中文" ;
//    	byte[] content = test.getBytes("utf8") ;
//    	content = test.getBytes("ascii");
//        return  ;
//    	long time = 10000L*24*60*60*1000 ;
//    	Timestamp stmp = new Timestamp(time) ;
//    	System.out.println(stmp.toGMTString()+"\n"+stmp.toLocaleString()) ;
//    	System.out.println(stmp.getDate()+" " + stmp.getMonth()+1+" " + stmp.getHours()) ;
    	System.out.println(new java.sql.Timestamp(1358804328437L)) ;
    	
    	double test = 1.02000000001 ;
    	System.out.println(String.format("%.3f", test)) ;
    	String a = "-" ;
    	System.out.println(a.substring(0)) ;
    	
    	System.out.println(new Double(test).longValue()) ;
    	System.out.println(100L/10000.0) ;
    }
    
    public static void reverse(char[] string, int start, int end)
    {
        char temp ;
        for (int i = 0 ; i <(end-start)/2 ; i ++)
        {
            temp = string[start+i] ;
            string[start+i] = string[end-i-1] ;
            string[end-i-1] = temp ;
        }
    }
    
    public static void move(char[] string, int m)
    {
        if (m>=string.length)
            m = string.length ;
        else if (m < 0)
            m = 0 ;
        
        reverse(string,0, m) ;
        System.out.println(string) ; 
        reverse(string, m, string.length) ;
        System.out.println(string) ; 
        reverse(string, 0, string.length) ;
        System.out.println(string) ; 
    }
    
    public static boolean isPrimeNumber(int number)
    {
        if (number == 1)
            return false ;
        
        if (number == 2)
            return true ;
        
        if (number % 2 == 0)
            return false ;
        
        int sqrt = (int)Math.sqrt(number) ;
        for (int i = 3 ; i <= sqrt ; i=i+2)
            if (number%i == 0)
                return false ;
        
        return true ;
    }
    
    /**
     * Compute count of Goldbach sum pairs. 
     * 
     * 
     * */
    public static int GoldbachConjecture(int num)
    {
        if (num % 2 == 1)
            return 0 ;
        
        return 0 ;
    }

    
}
